package com.xisoft.generator.controller;


import com.xisoft.common.aspect.AppMonitorRuning;
import com.xisoft.generator.service.ISysGeneratorService;
import com.xisoft.generator.utils.Query;
import com.xisoft.generator.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author xzh
 * @Date 2021/8/16
 * @description controller请求
 * 注意：本内容仅限于上海熙软科技有限公司内部，传阅，禁止外泄以及用于其他商业目的
 */
@RestController
@RequestMapping ("/sys/generator")
public class GenerationController
{
    @Qualifier ("SysGeneratorServiceImpl")
    @Autowired
    ISysGeneratorService service;

    /************************************************************* <br>
     *
     *说明: <br>
     *
     * @Description   <br>
     * @Date Created on 2021/11/22  15:21  <br>
     * @param params <br>
     * @return java.lang.String  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    @ResponseBody
    @RequestMapping ("/list")
    @AppMonitorRuning (method = "GenerationController.list", funEffect = "list！")
    public String list(@RequestParam Map< String,Object > params) throws Exception
    {
        String rst = service.queryList(new Query(params));
        return rst;
    }

    /************************************************************* <br>
     *
     *说明: 生成代码<br>
     *
     * @Description   <br>
     * @Date Created on 2021/11/22  15:21  <br>
     * @param response <br>
     * @param tables
     * @return void  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    @RequestMapping ("/code")
    @AppMonitorRuning (method = "GenerationController.getObjById", funEffect = "Toggle navigation代码搬运工！")
    public String create(String tables, HttpServletResponse response) throws Exception
    {
        ResultEntity resultEntity = new ResultEntity();
        try
        {
            //开始压缩并下载文件
            service.create(tables);

            // 远程部署需要下载到客户端到本地
            //  byte[] data = service.generatorCode();
            //  response.reset();
            //  response.setHeader("Content-Disposition", "attachment; filename=\"xisoft-code.zip\"");
            //  response.addHeader("Content-Length", "" + data.length);
            //  response.setContentType("application/octet-stream; charset=UTF-8");
            //  response.getOutputStream().write(data);
        }
        catch (Exception e)
        {
            resultEntity.put("code", "-1");
            resultEntity.put("msg", e.getMessage());
        }
        return resultEntity.toJSONString();
    }

    /************************************************************* <br>
     *
     *说明: 获取表字段属性<br>
     *
     * @Description   <br>
     * @Date Created on 2021/11/27  14:27  <br>
     * @param request
     * @param response <br>
     * @return void  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    @RequestMapping ("/dict")
    @AppMonitorRuning (method = "GenerationController.getColumns", funEffect = " 获取表字段属性！")
    public String getColumns(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return service.getColumns(request.getParameter("table")); }

    @RequestMapping ("/dictionary")
    @AppMonitorRuning (method = "GenerationController.dictionary", funEffect = " dictionary！")
    public ResultEntity dictionary(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return ResultEntity.ok().put("data", service.dictionary(request.getParameter("table"))); }
}